package com.cn.medicalcare.controller.servlet.admin;

import com.cn.medicalcare.controller.servlettool.MySessionContext;
import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.Admin;
import com.cn.medicalcare.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/updatapassword.json")
public class UpdataPasswordServlet extends HttpServlet {

    private AdminService adminService=AdminService.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String password=req.getParameter("password");
        String sessionId=req.getParameter("sessionId");
        HttpSession session= MySessionContext.getSession(sessionId);
        String username=((Admin)session.getAttribute("admin_user")).getUsername();
        Map<String,String> map=new LinkedHashMap<>();
        PrintWriter pw=resp.getWriter();
        if(password!=null && password.length()>4){
            Admin admin=new Admin().setUsername(username).setPassword(password);
            if(adminService.updataAdminPassword(admin)){
                //修改成功
                map.put("status","200");
            }else {
                map.put("status","500");
            }
        }else {
            map.put("status","400");
        }
        pw.print(JsonTool.toJson(map));
    }
}
